package binary.tests;

import binary.Binary16;
import binary.Binary8;
import junit.framework.TestCase;

public class Binary8Test extends TestCase {
	
	private void println(String S) {
		System.out.println(S);
	}
	
	private void printTitle(String S) {
		println("************************************************");
		println("***");
		println("*** " + S);
		println("***");
		println("************************************************");
	}
		
	@SuppressWarnings("unused")
	private void printItem(byte B) {
		printItem("",B);
	}
	private void printItem(String S, byte B) {
		println(S + Binary8.toBinaryString(B));
	}
	
	@SuppressWarnings("unused")
	private void printPair(byte B1, byte B2) {
		printPair("",B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printPair(String S1, byte B1, byte B2) {
		printPair(S1,B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printPair(byte B1, String S2, byte B2) {
		printPair("",B1,S2,B2);
	}
	
	private void printPair(String S1, byte B1, String S2, byte B2) {
		println(S1 + Binary8.toBinaryString(B1) + " : " + S2 + Binary8.toBinaryString(B2));
	}
	
	@SuppressWarnings("unused")
	private void printArray(byte B[]) {
		printArray("",B);
	}
	
	private void printArray(String S, byte B[]) {
		println(S + Binary8.toBinaryString(B));
	}
	
	@SuppressWarnings("unused")
	private void printArrays(byte B1[], byte B2[]) {
		printArrays("",B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printArrays(String S1, byte B1[], byte B2[]) {
		printArrays(S1,B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printArrays(byte B1[], String S2, byte B2[]) {
		printArrays("",B1,S2,B2);
	}
	
	
	private void printArrays(String S1, byte B1[], String S2, byte B2[]) {
		int		i;
		
		for(i=0; i<Math.max(B1.length, B2.length); i++) {
			S1 = String.format("Item[%3d] ==> ",i) + S1;
			printPair(S2, B1[i], S2, B2[i]);
		}
	}
	
	private byte[] testValues() {
		byte	B[] = new byte[(int) Math.pow(2,Binary8.NUMBITS)];
		int		i;
		
		for(i=0; i<B.length; i++) {
			B[i] = (byte) i; 
		}
		return B;
	}
	
	private byte[] testKeyValues() {
		byte	B[] = new byte[] {
				Binary8.TRUE,
				Binary8.FALSE,
				Binary8.LEFT,
				Binary8.RIGHT,
				Binary8.FIRST,
				Binary8.LAST
		};
		
		return B;
	}
	
	private byte[] testAllValue(byte V) {
		byte	B[] = new byte[(int) Math.pow(2,Binary8.NUMBITS)];
		Binary8.setAll(V, B);
		return B;
	}
	
	private boolean compareArray(byte A[], byte B[]) {
		if (A.length == B.length) {
			int	i;
			for(i=0; i<A.length; i++) {
				if (A[i] != B[i]) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	public final void testPrimitives() {
		printTitle("testPrimitives");
		byte	B1 = (byte) 0x55;
		
		boolean	R1;
		boolean	R2;
		int		i;
		
		printItem("B1    = ",B1);
		printItem("TRUE  = ",Binary8.TRUE);
		printItem("FALSE = ",Binary8.FALSE);
		printItem("FIRST = ",Binary8.FIRST);
		printItem("LAST  = ",Binary8.LAST);
		printItem("LEFT  = ",Binary8.LEFT);
		printItem("RIGHT = ",Binary8.RIGHT);
		
		
		
		for(i=0; i<Binary8.NUMBITS; i++) {
			R1	= Binary8.getBitFromRight(Binary8.NUMBITS-1-i, B1);
			R2	= Binary8.getBitFromLeft(i, B1);

			System.out.printf("R[%3d] == %5b     R[%3d] == %5b]\n", i, R1, i, R2);
			assertTrue(R1 == R2);
		}
	}
	
	public final void testReverseByte() {
		printTitle("testReverseByte");
		
		byte	B1 = Binary8.LEFT;
		byte	B2 = Binary8.reverse(B1);
		byte	B3 = Binary8.reverse(B2);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		
		assertTrue(B2 == Binary8.RIGHT);
		assertTrue(B3 == B1);
	}

	public final void testNotByte() {
		printTitle("testNotByte");

		byte	B1 = Binary8.FALSE;
		byte	B2 = Binary8.not(B1);
		byte	B3 = Binary8.not(B2);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);

		B2 = (byte) (B1 ^ B2);
		assertTrue(B2 == Binary8.TRUE);
		assertTrue(B3 == B1);
	}

	public final void testGetBitFromLeftIntByte() {
		printTitle("testGetBitFromLeftIntByte");
		printItem("FIRST = ", Binary8.FIRST);
		
		assertTrue(Binary8.getBitFromLeft(0, Binary8.FIRST));
		assertFalse(Binary8.getBitFromLeft(1, Binary8.FIRST));
		assertFalse(Binary8.getBitFromLeft(2, Binary8.FIRST));
		assertFalse(Binary8.getBitFromLeft(3, Binary8.FIRST));
		assertFalse(Binary8.getBitFromLeft(4, Binary8.FIRST));
		assertFalse(Binary8.getBitFromLeft(5, Binary8.FIRST));
		assertFalse(Binary8.getBitFromLeft(6, Binary8.FIRST));
		assertFalse(Binary8.getBitFromLeft(7, Binary8.FIRST));
	}

	public final void testGetBitFromRightIntByte() {
		printTitle("testGetBitFromRightIntByte");

		assertFalse(Binary8.getBitFromRight(0, Binary8.FIRST));
		assertFalse(Binary8.getBitFromRight(1, Binary8.FIRST));
		assertFalse(Binary8.getBitFromRight(2, Binary8.FIRST));
		assertFalse(Binary8.getBitFromRight(3, Binary8.FIRST));
		assertFalse(Binary8.getBitFromRight(4, Binary8.FIRST));
		assertFalse(Binary8.getBitFromRight(5, Binary8.FIRST));
		assertFalse(Binary8.getBitFromRight(6, Binary8.FIRST));
		assertTrue(Binary8.getBitFromRight(7, Binary8.FIRST));
	}

	public final void testSetBitFromLeftIntBooleanByte() {
		printTitle("testSetBitFromLeftIntBooleanByte");
		byte	B1 = Binary8.setBitFromLeft(0, true, Binary8.FALSE);
		byte	B2 = Binary8.setBitFromLeft(Binary8.NUMBITS-1, true, Binary8.FALSE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);

		assertTrue(B1 == Binary8.FIRST);
		assertTrue(B2 == Binary8.LAST);
	}

	public final void testSetBitFromRightIntBooleanByte() {
		printTitle("testSetBitFromRightIntBooleanByte");
		byte	B1 = Binary8.setBitFromRight(0, true, Binary8.FALSE);
		byte	B2 = Binary8.setBitFromRight(Binary8.NUMBITS-1, true, Binary8.FALSE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		
		assertTrue(B1 == Binary8.LAST);
		assertTrue(B2 == Binary8.FIRST);
	}

	public final void testGetAllFromLeft() {
		printTitle("testGetAllFromLeft");
		byte	B1	= Binary8.getAllFromLeft(Binary8.NUMBITS/2, Binary8.TRUE);
		byte	B2	= Binary8.getAllFromLeft(Binary8.NUMBITS, Binary8.TRUE);
		byte	B3	= Binary8.getAllFromLeft(1, Binary8.TRUE);
		byte	B4	= Binary8.getAllFromLeft(0, Binary8.TRUE);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		
		assertTrue(B1 == Binary8.LEFT);
		assertTrue(B2 == Binary8.TRUE);
		assertTrue(B3 == Binary8.FIRST);
		assertTrue(B4 == Binary8.FALSE);

	}

	public final void testGetAllFromRight() {
		printTitle("testGetAllFromRight");
		byte	B1	= Binary8.getAllFromRight(Binary8.NUMBITS/2, Binary8.TRUE);
		byte	B2	= Binary8.getAllFromRight(Binary8.NUMBITS, Binary8.TRUE);
		byte	B3	= Binary8.getAllFromRight(1, Binary8.TRUE);
		byte	B4	= Binary8.getAllFromRight(0, Binary8.TRUE);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		
		assertTrue(B1 == Binary8.RIGHT);
		assertTrue(B2 == Binary8.TRUE);
		assertTrue(B3 == Binary8.LAST);
		assertTrue(B4 == Binary8.FALSE);
	}

	public final void testSetAllFromLeft() {
		printTitle("testSetAllFromLeft");
		byte	B1 = Binary8.FALSE;
		byte	B2 = Binary8.setAllFromLeft(Binary8.NUMBITS/2, true, B1);
		byte	B3 = Binary8.setAllFromLeft(Binary8.NUMBITS/2, false, B2);
		byte	B4 = Binary8.setAllFromLeft(Binary8.NUMBITS, true, B1);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		
		assertTrue(B2 == Binary8.LEFT);
		assertTrue(B3 == Binary8.FALSE);
		assertTrue(B4 == Binary8.TRUE);
	}

	public final void testSetAllFromRight() {
		printTitle("testSetAllFromRight");
		byte	B1 = Binary8.FALSE;
		byte	B2 = Binary8.setAllFromRight(Binary8.NUMBITS/2, true, B1);
		byte	B3 = Binary8.setAllFromRight(Binary8.NUMBITS/2, false, B2);
		byte	B4 = Binary8.setAllFromRight(Binary8.NUMBITS, true, B1);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);

		assertTrue(B2 == Binary8.RIGHT);
		assertTrue(B3 == Binary8.FALSE);
		assertTrue(B4 == Binary8.TRUE);
	}

	public final void testGetBitsLeftToRight() {
		printTitle("testGetBitsLeftToRight");
		
		byte	B1 = Binary8.getBitsLeftToRight(0, Binary8.NUMBITS/2, Binary8.TRUE);
		byte	B2 = Binary8.getBitsLeftToRight(Binary8.NUMBITS/2, Binary8.NUMBITS/2, Binary8.TRUE);
		byte	B3 = Binary8.getBitsLeftToRight(0, 1, Binary8.TRUE);
		byte	B4 = Binary8.getBitsLeftToRight(Binary8.NUMBITS-1, 1, Binary8.TRUE);
		byte	B5 = Binary8.getBitsLeftToRight(0, 0, Binary8.TRUE);
		byte	B6 = Binary8.getBitsLeftToRight(0, Binary8.NUMBITS, Binary8.TRUE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		printItem("B5 = ", B5);
		printItem("B6 = ", B6);
		
		assertTrue(B1 == Binary8.LEFT);
		assertTrue(B2 == Binary8.RIGHT);
		assertTrue(B3 == Binary8.FIRST);
		assertTrue(B4 == Binary8.LAST);
		assertTrue(B5 == Binary8.FALSE);
		assertTrue(B6 == Binary8.TRUE);
	}

	public final void testGetBitsRightToLeft() {
		printTitle("testGetBitsRightToLeft");
		
		byte	B1 = Binary8.getBitsRightToLeft(0, Binary8.NUMBITS/2, Binary8.TRUE);
		byte	B2 = Binary8.getBitsRightToLeft(Binary8.NUMBITS/2, Binary8.NUMBITS/2, Binary8.TRUE);
		byte	B3 = Binary8.getBitsRightToLeft(0, 1, Binary8.TRUE);
		byte	B4 = Binary8.getBitsRightToLeft(Binary8.NUMBITS-1, 1, Binary8.TRUE);
		byte	B5 = Binary8.getBitsRightToLeft(0, 0, Binary8.TRUE);
		byte	B6 = Binary8.getBitsRightToLeft(0, Binary8.NUMBITS, Binary8.TRUE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		printItem("B5 = ", B5);
		printItem("B6 = ", B6);
		
		assertTrue(B1 == Binary8.RIGHT);
		assertTrue(B2 == Binary8.LEFT);
		assertTrue(B3 == Binary8.LAST);
		assertTrue(B4 == Binary8.FIRST);
		assertTrue(B5 == Binary8.FALSE);
		assertTrue(B6 == Binary8.TRUE);	
	}

	public final void testCopy() {
		printTitle("testCopy");
		
		byte	B[] = testValues();
		byte	C[] = Binary8.copy(B);
		
		printArray("B = ", B);
		printArray("B = ", C);
		assertTrue(compareArray(B,C));
		
	}

	public final void testReverseByteArray() {
		printTitle("testReverseByteArray");
		
		byte	B[];
		byte	R1[];
		byte	R2[];
		int		i;
		boolean	cmp;
		
		B = testValues();
		
		R1 = Binary8.copy(B);
		Binary8.reverse(R1);
		
		R2 = Binary8.copy(R1);
		Binary8.reverse(R2);
		
		for(i=0; i<B.length; i++) {
			cmp = (B[i] == R1[R1.length-1-i]);
			System.out.printf("B[%3d] vs R1[%3d] == %b\n", i, R1.length-1-i, cmp);
			assertTrue(cmp);
		}
		
		assertTrue(compareArray(B,R2));
	}

	public final void testReverseBits() {
		printTitle("testReverseBits");
		
		byte	B[];
		byte	R1[];
		byte	R2[];
		int		i;
		boolean	cmp;
		
		B = testValues();
		
		R1 = Binary8.copy(B);
		Binary8.reverseBits(R1);
		
		R2 = Binary8.copy(R1);
		Binary8.reverseBits(R2);
		
		for(i=0; i<B.length; i++) {
			cmp = (B[i] == Binary8.reverse( R1[R1.length-1-i]) );
			System.out.printf("B[%3d] vs R1[%3d] == %b\n", i, R1.length-1-i, cmp);
			assertTrue(cmp);
		}
		
		assertTrue(compareArray(B,R2));

	}

	public final void testGetBitFromLeftIntByteArray() {
		printTitle("testGetBitFromLeftIntByteArray");
		byte	B[] = testValues();
		int		i, j, k;
		boolean	V1, V2;
		
		k = 0;
		for(i=0; i<B.length; i++) {
			for(j=0; j<Binary8.NUMBITS; j++) {
				V1 = Binary8.getBitFromLeft(j, B[i]);
				V2 = Binary8.getBitFromLeft(k, B);
				System.out.printf("ArrayBit[%3d] : (Item[%3d], ItemBit[%3d]) == %5b : %5b\n", k, i, j, V1, V2);
				assertTrue(V1==V2);
				k++;
			}
		}
	}

	public final void testGetBitFromRightIntByteArray() {
		printTitle("testGetBitFromRightIntByteArray");
		byte	B[] = testKeyValues();
		int		i, j, k;
		boolean	V1, V2;
		
		printArray("B = ", B);

		k = 0;
		for(i=B.length-1; i>=0; i--) {
			for(j=0; j<Binary8.NUMBITS; j++) {
				V1 = Binary8.getBitFromRight(j, B[i]);
				V2 = Binary8.getBitFromRight(k, B);
				System.out.printf("ArrayBit[%3d] : (Item[%3d], Item[%3d]) == %5b : %5b\n", k, i, j, V1, V2);
				assertTrue(V1==V2);
				k++;
			}
		}	
	}

	public final void testSetBitFromLeftIntBooleanByteArray() {
		printTitle("testSetBitFromLeftIntBooleanByteArray");
		byte	B[] = testAllValue(Binary8.FALSE);
		int		i, j, k;
		
		k = 0;
		for(i=0; i<B.length; i++) {
			for(j=0; j<Binary8.NUMBITS; j++) {
				Binary8.setBitFromLeft(k, true, B);
				k++;
			}
		}
		
		for(i=0; i<B.length; i++) {
			String	S = String.format("Item[%d] = ", i);
			printItem(S,B[i]);
			assertTrue(B[i] == Binary8.TRUE);
		}
	}

	public final void testSetBitFromRightIntBooleanByteArray() {
		printTitle("testSetBitFromRightIntBooleanByteArray");
		byte	B[] = testAllValue(Binary8.FALSE);
		int		i, j, k;
		
		k = 0;
		for(i=0; i<B.length; i++) {
			for(j=0; j<Binary8.NUMBITS; j++) {
				Binary8.setBitFromRight(k, true, B);
				k++;
			}
		}
		
		for(i=0; i<B.length; i++) {
			String	S = String.format("Item[%d] = ", i);
			printItem(S,B[i]);
			assertTrue(B[i] == Binary8.TRUE);
		}
	}

	public final void testSetAll() {
		printTitle("testSetAll");
		byte	B[] = testValues();
		byte	V;
		int		i;
		
		V = Binary8.FALSE;
		Binary8.setAll(V, B);
		for(i=0; i<B.length; i++) {
			assertTrue(B[i] == V);
		}
		
		V = Binary8.TRUE;
		Binary8.setAll(V, B);
		for(i=0; i<B.length; i++) {
			assertTrue(B[i] == V);
		}
	}

	public final void testSetAllBits() {
		printTitle("testSetAllBits");
		byte	B[] = testValues();
		int		i;
		boolean	cmp;
		
		println("Setting values to anything but false");
		for(i=0; i<B.length; i++) {
			B[i] = Binary8.TRUE;
			cmp = (B[i] != Binary8.FALSE);
			System.out.printf("Checking B[%3d] != FALSE => %5b\n", i, cmp);
			assertTrue(cmp);
		}
		
		println("Setting all values to FALSE");
		Binary8.setAll(Binary8.FALSE, B);
		for(i=0; i<B.length; i++) {
			cmp = (B[i] == Binary8.FALSE);
			System.out.printf("Checking B[%3d] == FALSE => %5b\n", i, cmp);
			assertTrue(cmp);
		}
	}

	public final void testAnd() {
		printTitle("testAnd");
		byte	B1[] = testAllValue(Binary8.RIGHT);
		byte	B2[] = testAllValue(Binary8.LEFT);
		byte	B3[] = Binary8.and(B1, B2);
		byte	B4[] = Binary8.and(B1, B1);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary8.FALSE);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary8.RIGHT);
		}
		
	}

	public final void testOr() {
		printTitle("testOr");
		byte	B1[] = testAllValue(Binary8.RIGHT);
		byte	B2[] = testAllValue(Binary8.LEFT);
		byte	B3[] = Binary8.or(B1, B2);
		byte	B4[] = Binary8.or(B1, B1);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary8.TRUE);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary8.RIGHT);
		}
	}

	public final void testXor() {
		printTitle("testXor");
		byte	B1[] = testAllValue(Binary8.RIGHT);
		byte	B2[] = testAllValue(Binary8.LEFT);
		byte	B3[] = Binary8.xor(B1, B2);
		byte	B4[] = Binary8.xor(B1, B1);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary8.TRUE);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary8.FALSE);
		}
	}

	public final void testNotByteArray() {
		printTitle("testNotByteArray");
		byte	B1[] = testAllValue(Binary8.RIGHT);
		byte	B2[] = testAllValue(Binary8.LEFT);
		byte	B3[] = Binary8.not(B1);
		byte	B4[] = Binary8.not(B2);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary8.LEFT);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary8.RIGHT);
		}
	}

	public final void testShiftRight() {
		//
		// TODO: Make this more efficient so we are shifting more than one bit at a time
		//
		printTitle("testShiftLeft");
		
		byte	B[] = testKeyValues();
		byte	C[];
		int		i, numBits, shift;
		
		shift = 7;
		
		numBits = B.length*Binary8.NUMBITS;
		
		printArray("B == ", B);
		
		C = Binary8.shiftLeft(shift, B);
		printArray("C == ", C);
		
		for(i=0; i<numBits-shift; i++) {
			assertTrue(	Binary8.getBitFromLeft(i, C) ==	Binary8.getBitFromLeft(i + shift, B) );
		}
		for(i=numBits-shift; i<numBits; i++) {
			assertTrue(	Binary8.getBitFromLeft(i, C) ==	false );
		}
	}

	public final void testShiftLeft() {
		//
		// TODO: Make this more efficient so we are shifting more than one bit at a time
		//
		printTitle("testShiftLeft");
		
		byte	B[] = testKeyValues();
		byte	C[];
		int		i, numBits, shift;
		
		shift = 7;
		
		numBits = B.length*Binary8.NUMBITS;
		
		printArray("B == ", B);
		
		C = Binary8.shiftLeft(shift, B);
		printArray("C == ", C);
		
		for(i=0; i<numBits-shift; i++) {
			assertTrue(	Binary8.getBitFromLeft(i, C) ==	Binary8.getBitFromLeft(i + shift, B) );
		}
		for(i=numBits-shift; i<numBits; i++) {
			assertTrue(	Binary8.getBitFromLeft(i, C) ==	false );
		}
	}

	public final void testToBinaryStringByte() {
		printTitle("testToBinaryStringByte");
		
		String	tmp;

		tmp = Binary8.toBinaryString(Binary8.TRUE);
		println("tmp == " + tmp);
		assertTrue("11111111".compareTo(tmp)==0);
		
		tmp = Binary8.toBinaryString(Binary8.FALSE);
		println("tmp == " + tmp);
		assertTrue("00000000".compareTo(tmp)==0);
		
		tmp = Binary8.toBinaryString(Binary8.LEFT);
		println("tmp == " + tmp);
		assertTrue("11110000".compareTo(tmp)==0);
		
		tmp = Binary8.toBinaryString(Binary8.RIGHT);
		println("tmp == " + tmp);
		assertTrue("00001111".compareTo(tmp)==0);
		
		tmp = Binary8.toBinaryString(Binary8.FIRST);
		println("tmp == " + tmp);
		assertTrue("10000000".compareTo(tmp)==0);
		
		tmp = Binary8.toBinaryString(Binary8.LAST);
		println("tmp == " + tmp);
		assertTrue("00000001".compareTo(tmp)==0);
		
	}

	public final void testToHexStringByte() {
		printTitle("testToHexStringByte");
		
		String	tmp;

		tmp = Binary8.toHexString(Binary8.TRUE);
		println("tmp == " + tmp);
		assertTrue("FF".compareTo(tmp)==0);
		
		tmp = Binary8.toHexString(Binary8.FALSE);
		println("tmp == " + tmp);
		assertTrue("00".compareTo(tmp)==0);
		
		tmp = Binary8.toHexString(Binary8.LEFT);
		println("tmp == " + tmp);
		assertTrue("F0".compareTo(tmp)==0);
		
		tmp = Binary8.toHexString(Binary8.RIGHT);
		println("tmp == " + tmp);
		assertTrue("0F".compareTo(tmp)==0);
		
		tmp = Binary8.toHexString(Binary8.FIRST);
		println("tmp == " + tmp);
		assertTrue("80".compareTo(tmp)==0);
		
		tmp = Binary8.toHexString(Binary8.LAST);
		println("tmp == " + tmp);
		assertTrue("01".compareTo(tmp)==0);
		
	}

	public final void testToQuadStringByte() {
		printTitle("testToQuadStringByte");
		
		String	tmp;

		tmp = Binary8.toQuadString(Binary8.TRUE);
		println("tmp == " + tmp);
		assertTrue("3333".compareTo(tmp)==0);
		
		tmp = Binary8.toQuadString(Binary8.FALSE);
		println("tmp == " + tmp);
		assertTrue("0000".compareTo(tmp)==0);
		
		tmp = Binary8.toQuadString(Binary8.LEFT);
		println("tmp == " + tmp);
		assertTrue("3300".compareTo(tmp)==0);
		
		tmp = Binary8.toQuadString(Binary8.RIGHT);
		println("tmp == " + tmp);
		assertTrue("0033".compareTo(tmp)==0);
		
		tmp = Binary8.toQuadString(Binary8.FIRST);
		println("tmp == " + tmp);
		assertTrue("2000".compareTo(tmp)==0);
		
		tmp = Binary8.toQuadString(Binary8.LAST);
		println("tmp == " + tmp);
		assertTrue("0001".compareTo(tmp)==0);
		
	}

	public final void testToOctalStringByte() {
		printTitle("testToOctalStringByte");
		
		String	tmp;

		tmp = Binary8.toOctalString(Binary8.TRUE);
		println("tmp == " + tmp);
		assertTrue("377".compareTo(tmp)==0);
		
		tmp = Binary8.toOctalString(Binary8.FALSE);
		println("tmp == " + tmp);
		assertTrue("000".compareTo(tmp)==0);
		
		tmp = Binary8.toOctalString(Binary8.LEFT);
		println("tmp == " + tmp);
		assertTrue("360".compareTo(tmp)==0);
		
		tmp = Binary8.toOctalString(Binary8.RIGHT);
		println("tmp == " + tmp);
		assertTrue("017".compareTo(tmp)==0);
		
		tmp = Binary8.toOctalString(Binary8.FIRST);
		println("tmp == " + tmp);
		assertTrue("200".compareTo(tmp)==0);
		
		tmp = Binary8.toOctalString(Binary8.LAST);
		println("tmp == " + tmp);
		assertTrue("001".compareTo(tmp)==0);
		
	}

	public final void testToBinaryStringByteArray() {
		printTitle("testToBinaryStringByteArray");
		byte	B[] = testKeyValues();
		String	S = Binary8.toBinaryString(B);
		String	E = "\n11111111\n00000000\n11110000\n00001111\n10000000\n00000001\n";
		
		println("S == " + S);
		println("E == " + E);
		
		assertTrue(	S.equals(E) );
	}

	public final void testToHexStringByteArray() {
		printTitle("testToHexStringByteArray");
		byte	B[] = testKeyValues();
		String	S = Binary8.toHexString(B);
		String	E = "\nFF\n00\nF0\n0F\n80\n01\n";
		
		println("S == " + S);
		println("E == " + E);
		
		assertTrue(	S.equals(E) );
	}

	public final void testToOctalStringByteArray() {
		printTitle("testToOctalStringByteArray");
		byte	B[] = testKeyValues();
		String	S = Binary8.toOctalString(B);
		String	E = "\n377\n000\n360\n017\n200\n001\n";
		
		println("S == " + S);
		println("E == " + E);
		
		assertTrue(	S.equals(E) );
	}

	public final void testToArrayShortArray() {
		printTitle("testToArrayShortArray");
		byte B[] = testKeyValues();
		
		short A[] = new short[] {
			(short) 0xFF00,
			(short) 0xF00F,
			(short) 0x8001
		};
		printArray("B = ", B);

		byte T[] = Binary8.toArray(A);
		
		println("A = ");
		println(Binary16.toBinaryString(A));
		
		printArray("T = ", T);
		
		assertTrue(compareArray(B,T));
	}

	public final void testToArrayIntArray() {
		printTitle("testToArrayIntArray");
		int A[] = new int[] {
			(int) 0x0000FF00,
			(int) 0xF00F8001
		};
		byte B[] = new byte[] {
				Binary8.FALSE,
				Binary8.FALSE,
				Binary8.TRUE,
				Binary8.FALSE,
				Binary8.LEFT,
				Binary8.RIGHT,
				Binary8.FIRST,
				Binary8.LAST
		};
		byte T[] = Binary8.toArray(A);
		
		printArray("B = ", B);
		printArray("T = ", T);
		
		assertTrue(compareArray(B,T));
	}

	public final void testToArrayLongArray() {
		printTitle("testToArrayLongArray");
		long A[] = new long[] {
			(long) 0x0000FF00F00F8001L
		};
		byte B[] = new byte[] {
				Binary8.FALSE,
				Binary8.FALSE,
				Binary8.TRUE,
				Binary8.FALSE,
				Binary8.LEFT,
				Binary8.RIGHT,
				Binary8.FIRST,
				Binary8.LAST
		};
		byte T[] = Binary8.toArray(A);
		
		printArray("B = ", B);
		printArray("T = ", T);
		
		assertTrue(compareArray(B,T));
	}

}
